package bb.lanxing.calc.calculator;

import com.github.mikephil.charting.utils.Utils;
import bb.lanxing.util.Log;
import java.util.Locale;

public class PowerAnalyzer {
    private static final String TAG = "PowerAnalyzer";
    private double accumulatedPower;
    private long accumulatedPowerCount;
    private double avgPower;
    private double ftp;
    private double intensityFactor;
    private double maxPower;
    private double normalizedPower;
    private double normalizedPowerPow4;
    private final double[] powerBuf = new double[30];
    private double powerFor10Seconds;
    private double powerFor30Seconds;
    private double powerFor3Seconds;
    private double tss;

    private double vi;

    public PowerAnalyzer(double ftp) {
        this.ftp = ftp;
    }

    public void update(Double power) {
        Log.d(TAG, "update. power=" + power);
        if (power == null || power == Utils.DOUBLE_EPSILON) {
            return;
        }
        this.accumulatedPowerCount++;
        this.accumulatedPower += power;
        Log.d(TAG, "update. this.accumulatedPower=" + this.accumulatedPower);
        this.maxPower = Math.max(power, this.maxPower);
        long j = this.accumulatedPowerCount;
        int i = (int) (j % 30);
        double d2 = this.accumulatedPower;
        this.avgPower = d2 / j;
        double[] dArr = this.powerBuf;
        double d3 = (d2 - dArr[i]) / 30.0d;
        this.powerFor30Seconds = d3;
        this.powerFor10Seconds = (d2 - dArr[(i + 20) % 30]) / 10.0d;
        this.powerFor3Seconds = (d2 - dArr[(i + 27) % 30]) / 3.0d;
        dArr[i] = d2;
        if (j > 30) {
            double d4 = j - 30;
            double pow = this.normalizedPowerPow4 + ((Math.pow(d3, 4.0d) - this.normalizedPowerPow4) / d4);
            this.normalizedPowerPow4 = pow;
            double sqrt = Math.sqrt(Math.sqrt(pow)) * 10.0d;
            if (sqrt % 10.0d > 4.0d) {
                this.normalizedPower = (sqrt / 10.0d) + 1.0d;
            } else {
                this.normalizedPower = sqrt / 10.0d;
            }
            double d5 = this.normalizedPower;
            double d6 = this.ftp;
            double d7 = (d5 * 1000.0d) / d6;
            this.intensityFactor = d7;
            this.tss = ((d4 * d5) * (d7 / 1000.0d)) / (d6 * 36.0d);
            this.vi = d5 / this.avgPower;
        }
        Log.d(TAG, String.format(Locale.getDefault(), "POWER(%f), 30AVG(%f),normalizedPowerPow4(%f), IF(%f), TSS(%f), NP(%f),VI(%f)", power, this.powerFor30Seconds, this.normalizedPowerPow4, this.intensityFactor / 1000.0d, this.tss, this.normalizedPower, this.vi));
    }

    public double getTss() {
        return this.tss;
    }

    public double getMaxPower() {
        return this.maxPower;
    }

    public double getPowerFor3Seconds() {
        return this.powerFor3Seconds;
    }

    public double getPowerFor10Seconds() {
        return this.powerFor10Seconds;
    }

    public double getAvgPower() {
        return this.avgPower;
    }

    public double getPowerFor30Seconds() {
        return this.powerFor30Seconds;
    }

    public double getIf() {
        return this.intensityFactor / 1000.0d;
    }

    public double getNormalizedPower() {
        return this.normalizedPower;
    }

    public double getVi() {
        return this.vi;
    }

    public double getFtp() {
        return this.ftp;
    }

    public void setFtp(double d) {
        this.ftp = d;
    }
}
